#include <QMouseEvent>
#include <QEvent>
#include <QMessageBox>
#include "Block.h"
#include "config.h"

Block::Block(QWidget *widget) : label(widget), button(widget) {
    setStyle();
    connect(&button, &QPushButton::clicked, this, &Block::onClick);
    button.installEventFilter(this);
    button.show();
    label.show();
}

Block &Block::setValue(int v) {
    value = v;
    if (v == -1) {
        label.setText("💣");
    } else if (v == 0) {
        label.setText("");
    } else {
        label.setText(QString::number(v));
    }
    setStyle();
    return *this;
}

int Block::getValue() const {
    return value;
}

Block &Block::setPosition(int x_, int y_) {
    x = x_;
    y = y_;
    button.setGeometry(x * BLOCK_WIDTH, y * BLOCK_HEIGHT, BLOCK_WIDTH, BLOCK_HEIGHT);
    label.setGeometry(x * BLOCK_WIDTH, y * BLOCK_HEIGHT, BLOCK_WIDTH, BLOCK_HEIGHT);
    return *this;
}

std::pair<int, int> Block::getPosition() {
    return {x, y};
}

void Block::setWrong() {
    button.setText("❌");
}

void Block::setRed() {
    label.setStyleSheet(label.styleSheet() + "background:#ff0000;");
}

void Block::press() {
    if (m_state == State::PRESSED) {
        return;
    }
    m_state = State::PRESSED;
    button.hide();
}

void Block::mark() {
    if (m_state == State::NORMAL) {
        m_state = State::MARKED;
        button.setText("🚩");
    } else if (m_state == State::MARKED) {
        m_state = State::UNKNOWN;
        button.setText("❓");
    } else if (m_state == State::UNKNOWN) {
        m_state = State::NORMAL;
        button.setText("");
    }
}

void Block::reset() {
    m_state = State::NORMAL;
    button.setText("");
    button.show();
    setValue(0);
}

bool Block::isPressed() const {
    return state == State::PRESSED;
}

void Block::onClick() {
    if (state != State::MARKED) {
        emit pressed(this);
    }
}

void Block::setStyle() {
    static const QString colors[9] = {
            "#0000ff", "#007f00", "#ff0000", "#7f007f",
            "#7f3f00", "#007f7f", "#000000", "#7f7f7f"
    };
    label.setAlignment(Qt::AlignCenter);
    label.setStyleSheet(QStringLiteral(
                                "font-family:Consolas;"        // 等宽字体
                                "font-weight:bold;"
                                "font-size:20px;"
                                "color:%1;"                    // 数字颜色
                                "background:#c0c0c0;"          // 方块背景
                                "border:2px solid #808080;"    // 实线边框
                        ).arg(colors[qBound(1, value, 8) - 1]));
    button.setStyleSheet(
            "QPushButton{"
            "  background: qlineargradient("
            "      x1:0, y1:0, x2:1, y2:1,"
            "      stop:0 #d0d0d0, stop:1 #b0b0b0);"   // 灰色渐变
            "  border: 4px outset #a0a0a0;"            // 立体外凸边框
            "  border-radius: 0px;"
            "}"
            "QPushButton:pressed{"                     // 按下时凹陷
            "  border: 4px inset #808080;"
            "  background: #b0b0b0;"
            "}"
    );
}

bool Block::eventFilter(QObject *obj, QEvent *ev) {
    if (obj == &button) {
        if (ev->type() == QEvent::MouseButtonPress) {
            auto *me = dynamic_cast<QMouseEvent *>(ev);
            if (me->button() == Qt::RightButton) {
                if (!isPressed()) {
                    emit marked(this);
                }
                return true;               // 已处理
            }
        } else if (ev->type() == QEvent::Enter) {
            emit entered(this);
        } else if (ev->type() == QEvent::Leave) {
            emit leaved(this);
        }
    }
    return QObject::eventFilter(obj, ev);
}